package com.outstudio.plan.op.pdf;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.outstudio.plan.op.pdf.model.Furniture;
import com.outstudio.plan.op.pdf.model.HomePage;
import com.outstudio.plan.op.pdf.model.Room;

/**
 * 生成pdf各页的内容，即将svg解析后的对象装入pdf中
 * 
 * @author zhagz
 * 
 */
public class PDFPageCreater {

	private PdfCreater pc;
	private String absPath;
	private String pdfName;

	public void start(String absPath, String pdfName) {
		this.absPath = absPath;
		File p = new File(absPath);
		if (!p.exists()) {
			p.mkdirs();
		}
		this.pdfName = pdfName;
		pc = new PdfCreater();
	}

	public void finish() {
		pc.closePdf();
	}

	public void createMainPage(String biaoti, String description, String imagePath) throws DocumentException,
			IOException {
		Document doc = pc.createPdf(absPath + "/" + pdfName);
		pc.addParagraph(biaoti, 30, BaseColor.GRAY);
		pc.addDescription(description, 15, BaseColor.BLACK);
		pc.addLineSeparator();

		pc.addMainPageImage(imagePath);
		pc.addLineSeparator();

		pc.addParagraph("平面图", 30, BaseColor.GRAY);

		pc.newPage();
	}

	/**
	 * 生成主页的方法
	 * 
	 * @param hp
	 * @throws IOException
	 * @throws DocumentException
	 */
	public void createMainPage(HomePage hp) throws IOException, DocumentException {
		Document doc = pc.createPdf(absPath + "/" + pdfName);
		pc.addParagraph(hp.getBiaoti(), 30, BaseColor.GRAY);
		pc.addDescription(hp.getDescription(), 15, BaseColor.BLACK);
		pc.addLineSeparator();
		pc.addMainPageImage(hp.getImage());

		pc.addLineSeparator();

		pc.addParagraph("平面图", 30, BaseColor.GRAY);
		pc.addLineSeparator(0, 30, BaseColor.BLACK, Element.ALIGN_LEFT, -5);
		if (hp.getName() != null) {
			pc.addParagraph("项目名：" + hp.getName(), 12, BaseColor.BLACK);
			pc.getDocument().addTitle(hp.getName());
		}
		if (hp.getAuthor() != null) {
			pc.addParagraph("作者：" + hp.getAuthor(), 12, BaseColor.BLACK);
			pc.getDocument().addAuthor(hp.getAuthor());
		}
		if (hp.getContact() != null) {
			pc.addParagraph("联系人：" + hp.getContact(), 12, BaseColor.BLACK);
		}
		if (hp.getEmail() != null) {
			pc.addParagraph("邮箱：" + hp.getEmail(), 12, BaseColor.BLACK);
		}
		if (hp.getPhone() != null) {
			pc.addParagraph("联系电话：" + hp.getPhone(), 12, BaseColor.BLACK);
		}
		if (hp.getAddress() != null) {
			pc.addParagraph("地址：" + hp.getAddress(), 12, BaseColor.BLACK);
		}
		if (hp.getDate() != null) {
			pc.addParagraph("创建日期：" + hp.getDate(), 12, BaseColor.BLACK);
		}
		if (hp.getText() != null) {
			pc.addParagraph("备注：" + hp.getText(), 12, BaseColor.BLACK);
		}

		pc.newPage();
	}

	/**
	 * 生成文章正文的方法
	 * 
	 * @param room
	 * @throws DocumentException
	 * @throws IOException
	 */
	public void createNewPage(Room room) throws DocumentException, IOException {

		pc.addParagraph(room.getName(), 30, BaseColor.GRAY);
		pc.addDescription(room.getDescription(), 15, BaseColor.BLACK);
		pc.addLineSeparator();

		// img.scaleToFit(PageSize.A4);
		// img.scalePercent(50);
		// img.setAlignment(Image.RIGHT | Image.TEXTWRAP);
		// pc.addElement(img);

		/***** 只有描述数据的小table **********/
		Map<String, String> map = room.getData();
		PdfPCell cell = null;
		PdfPTable dataTable = new PdfPTable(2);

		if (map != null && !map.isEmpty()) {
			Set<String> set = map.keySet();
			Iterator<String> it = set.iterator();
			System.out.println(map.size());
			while (it.hasNext()) {
				String key = it.next();
				cell = pc.createCell(key + ":", 10);
				dataTable.addCell(cell);
				cell = pc.createCell(map.get(key), 10);
				dataTable.addCell(cell);
				System.out.println(key + ":" + map.get(key));
			}
		}
		/**** 有房间图片的大的table *******************/
		PdfPTable pt = new PdfPTable(5);

		// pt.setTotalWidth(PageSize.A4.getBorderWidth());

		pt.getDefaultCell().setBorder(0);

		cell = pc.createCell(dataTable);
		cell.setColspan(2);
		pt.addCell(cell);

		Image image = Image.getInstance(room.getImage());
		Image thumbnail = Image.getInstance(room.getThumbnail());

		image.setAlignment(Image.RIGHT);
		cell = pc.createCell(image);
		cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
		cell.setRowspan(4);
		cell.setColspan(3);
		pt.addCell(cell);
		cell = pc.createCell(thumbnail);
		cell.setColspan(2);
		cell.setRowspan(4);
		cell.setUseAscender(true);
		cell.setHorizontalAlignment(Element.ALIGN_LEFT);
		cell.setVerticalAlignment(Element.ALIGN_LEFT);
		pt.addCell(cell);

		pc.addElement(pt);
		pc.addLineSeparator();

		/******* 家具的table *********/
		/*
		 * 1行，5列 1格：ficon，图标 1格：name，名字 2格：fimg，实物图
		 */
		List<Furniture> list = room.getFurns();
		int index = 0;
		if (list != null) {
			index = list.size();
		}
		PdfPTable fp = null;
		Furniture f = null;

		for (int i = 0; i < index; i++) {
			f = list.get(i);
			// icon
			Image ficon = null;
			if (f.getIcon() != null) {
				ficon = Image.getInstance(f.getIcon());
				ficon.scaleAbsolute(50, 50);
				ficon.setAlignment(Image.ALIGN_CENTER);
			}
			// img
			Image fimg = null;
			System.out.println("f.getImage is " + f.getImage());
			if (f.getImage() != null) {
				fimg = Image.getInstance(f.getImage());
			}

			fp = new PdfPTable(7);
			fp.setHorizontalAlignment(Element.ALIGN_CENTER);

			// 图标
			cell = pc.createCell(ficon);
			cell.setColspan(1);
			cell.setPadding(5f);
			cell.setHorizontalAlignment(Element.ALIGN_CENTER);
			cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
			fp.addCell(cell);

			// 名字
			System.out.println("itemName:" + f.getName() + ",item_unique:" + f.getItem_unique());
			if (null == f.getName() || "".equals(f.getName()) || "null".equals(f.getName())) {
				cell = pc.createCell(f.getItem_unique(), 12);
			} else {
				cell = pc.createCell(f.getName(), 12);
			}
			cell.setNoWrap(false);// 设定折行
			cell.setColspan(1);
			cell.setPadding(5f);
			fp.addCell(cell);

			// text
			cell = pc.createCell(f.getText() == "" ? f.getText() : "无", 12);
			cell.setColspan(1);
			cell.setPadding(5f);
			cell.setNoWrap(false);// 设定折行
			fp.addCell(cell);

			// 价格
			cell = pc.createCell(f.getPrice() == "" ? f.getPrice() : "￥0.00", 12);
			cell.setColspan(1);
			cell.setPadding(5f);
			cell.setNoWrap(false);// 设定折行
			fp.addCell(cell);

			// 数量
			cell = pc.createCell(f.getNumber() == "" ? f.getNumber() : "1个", 12);
			cell.setColspan(1);
			cell.setPadding(5f);
			cell.setNoWrap(false);// 设定折行
			fp.addCell(cell);

			// 实物图
			cell = pc.createCell(fimg);
			cell.setColspan(2);
			cell.setPadding(5f);
			cell.setHorizontalAlignment(Element.ALIGN_CENTER);
			cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
			fp.addCell(cell);

			pc.addElement(fp);

			pc.addLineSeparator();
		}

		pc.newPage();
	}

}
